package com.fancy.learncenter.ui.fragment;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.andview.refreshview.XScrollView;
import com.fancy.learncenter.R;
import com.fancy.learncenter.bean.BaseBean;
import com.fancy.learncenter.bean.ShowBannerDataBean;
import com.fancy.learncenter.net.CustomNetCallBack;
import com.fancy.learncenter.net.CustomNetSubscriber;
import com.fancy.learncenter.net.HttpMehtod;
import com.fancy.learncenter.ui.adapter.FragPagerAdapter;
import com.fancy.learncenter.ui.adapter.FragmentShowBannerAdapter;
import com.fancy.learncenter.utils.NetUtil;
import com.fancy.learncenter.utils.Utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.ResponseBody;
import retrofit2.Response;


public class ShowFragment extends Fragment {

    View rootView;
    @Bind(R.id.gridView)
    RecyclerView gridView;
    @Bind(R.id.view_pager)
    ViewPager viewPager;
    @Bind(R.id.top_LL)
    LinearLayout topLL;
    @Bind(R.id.titile)
    TextView titile;
    @Bind(R.id.tab_layout)
    TabLayout tabLayout;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        rootView = inflater.inflate(R.layout.fragment_show, container, false);
        ButterKnife.bind(this, rootView);
        initGridView();
        initViewPager();
        getBannerData();

        return rootView;
    }

    private void initViewPager() {
        List<Fragment> fragments = new ArrayList<>();
        fragments.add(new ShowVideoFragment());
        fragments.add(new ShowAudioFragment());

        List titles = new ArrayList();
        titles.add("视频");
        titles.add("音频");
        FragPagerAdapter mFragmentAdapteradapter =
                new FragPagerAdapter(getChildFragmentManager(), fragments, titles);
        //给ViewPager设置适配器
        viewPager.setAdapter(mFragmentAdapteradapter);

        tabLayout.setTabMode(TabLayout.MODE_FIXED);
        //为TabLayout添加tab名称
        tabLayout.addTab(tabLayout.newTab());
        tabLayout.addTab(tabLayout.newTab());
        tabLayout.setupWithViewPager(viewPager);


    }


    FragmentShowBannerAdapter bannerAdapter;
    List<ShowBannerDataBean.CategoryBean.CategoryListBean> bannerData;

    private void initGridView() {
        bannerData = new ArrayList<>();

        GridLayoutManager manager = new GridLayoutManager(getActivity(), 4);
        gridView.setLayoutManager(manager);
        bannerAdapter = new FragmentShowBannerAdapter(getActivity(), bannerData);
        gridView.setAdapter(bannerAdapter);
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    private void getBannerData() {
        Map<String, Object> paramer = new HashMap<>();
        paramer.put("id", "0");
        paramer.put("type", "1");
        paramer.put("hasTag", true);

        HttpMehtod.getInstance().getShowBanner(paramer, new CustomNetSubscriber(getActivity()) {
            @Override
            public void onNext(Response<ResponseBody> responseBodyResponse) {
                //解析数据
                BaseBean<ShowBannerDataBean> baseBean = Utils.fromJsonObject(NetUtil.getResResult(responseBodyResponse), ShowBannerDataBean.class);
                //数据业务逻辑处理
                CustomNetCallBack callBack = new CustomNetCallBack(getContext(), baseBean);
                callBack.setCallBack(new CustomNetCallBack.NetCallBack<ShowBannerDataBean>() {
                    @Override
                    public void success(ShowBannerDataBean dataBean) {
                        bannerData.addAll(dataBean.getCategory().getCategoryList());

                    }

                    @Override
                    public void fail(String errorCode) {


                    }

                }).dealData();
            }

            @Override
            public void onCompleted() {
                super.onCompleted();
                bannerAdapter.notifyDataSetChanged();
            }
        });
    }


}
